#ifndef MYANIMATION_H
#define MYANIMATION_H
#include <QPropertyAnimation>
#include <QGraphicsOpacityEffect>
#include <QDebug>
#include <QRect>
#include <QTimer>

#include "common.h"

//专门用来重写缩放的动画做成线性变化
class MyAnimationScale : public QPropertyAnimation
{
public:
    explicit MyAnimationScale(QObject *target, const QByteArray &propertyName, QObject *parent = Q_NULLPTR);
    ~MyAnimationScale();
    void Start();
    void SetStartValue(QVariant value);
    void SetEndValue(QVariant value);
    void SetDuration(int duration);
    void SetFrameNum(int num);
    void Stop();

protected:

    //此函数重写帧筛选的过程，自己实现每一帧的变化，并返回渲染
    QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const
    {
//        LOG_QDEBUG << from;
//        LOG_QDEBUG << to;
//        LOG_QDEBUG << "progress===" << progress;
        QRect rect_from = from.toRect();
        QRect rect_to = to.toRect();
        QVariant var;


        if (rect_from.width() > rect_to.width())
        {
//            LOG_QDEBUG << rect_from;
//            LOG_QDEBUG << rect_to;
            int interval_x = qRound((rect_to.x() - rect_from.x())*progress);
            int interval_y = qRound((rect_to.y() - rect_from.y())*progress);
            //LOG_QDEBUG << interval_x << " " << interval_y;
            var = QRect(rect_from.x() + interval_x,
                       rect_from.y() + interval_y,
                       rect_from.width() - (rect_from.x() + interval_x)*2,
                       rect_from.height() - (rect_from.y() + interval_y)*2);
        }
        else
        {
//            LOG_QDEBUG << rect_from;
//            LOG_QDEBUG << rect_to;
            int interval_x = qRound((rect_from.x() - rect_to.x())*progress);
            int interval_y = qRound((rect_from.y() - rect_to.y())*progress);
            //LOG_QDEBUG << interval_x << " " << interval_y;
            var = QRect(rect_from.x() - interval_x,
                       rect_from.y() - interval_y,
                       rect_to.width() - (rect_from.x() - interval_x)*2,
                       rect_to.height() - (rect_from.y() - interval_y)*2);
        }
//        LOG_QDEBUG << var;
        return var;
    }

public slots:
    void UpdateProgressTime();

private:

    //动画计时器控制帧率
    QTimer m_progress_timer;

    //动画起始位置
    QVariant m_start_value;
    QVariant m_end_value;

    //当前时间
    int m_current_time;
    //动画总时间
    int m_duration;
    //动画总帧数
    int m_frame_num;
};

//动画类
class HqLabel;
class MyAnimation : public QPropertyAnimation
{
public:
    explicit MyAnimation(HqLabel *target);
    ~MyAnimation();
    void RunHqtAnimation(QString place_way, Animation animation);
    void ReRunHqtRotateGrabAnimation(Animation animation);

    void RunPngAnimation(Animation animation);

    void RunLeftRightAnimation();
    void RunOpacityEffectAnimation();
    void Run2FrameScaleAnimation();
    void RunScaleAnimation();

    void UpdateCurrentValue(const QVariant &value);
    void UpdateCurrentState(QAbstractAnimation::State state);

    void SetSize(int w, int h);
    void RestoreAnimation();

private:
    HqLabel *m_target;
    MyAnimationScale *m_scale_animation;
};

#endif // MYANIMATION_H
